import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)

//准备actions对象——响应组件中用户的动作
const actions = {
	//jia和jian都是无意义的action，所以就不写
	/* 
		jia(miniStore,value){
			miniStore.commit('JIA',value)
		},
		jian({commit},value){
			commit('JIAN',value)
		}
	*/
	//jiaOdd和jiaWait都是有意义的action，所以要写
	jiaOdd(miniStore,value){
		if(miniStore.state.sum % 2){
			miniStore.commit('JIA',value)
		}
	},
	jiaWait({commit},value){
		setTimeout(()=>{
			commit('JIA',value)
		},500)
	},
	jiaAjax(miniStore){
		axios.get('https://api.uixsj.cn/hitokoto/get?type=social').then(
			response => {
				console.log(response.data)
				miniStore.commit('JIA',response.data.length)
			},
			error => {
				alert(error.message)
			}
		)
	}
}
//准备mutations对象——修改state中的数据
const mutations = {
	JIA(state,value){
		state.sum += value
	},
	JIAN(state,value){
		state.sum -= value
	}
}
//准备state对象——初始化数据
const state = {
	sum:0,
	description:'可以完成简单的加、减'
}

const getters = {
	bigSum(state){
		return state.sum * 10
	}
}

const store = new Vuex.Store({
	actions,
	mutations,
	state,
	getters
})

export default store

